﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public   class OControlTicketDAL
    {
        public static bool Add(OControlTicketInfo obj,List<OControlTicketItemInfo> listItem)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OControlTicket_Add", obj.MachineType,obj.TNO,obj.SupplierName,obj.ControlReason,obj.OpenDate,obj.ApprovalDate,obj.SerialNumber1,obj.SerialNumber2,obj.SerialNumber3,obj.SerialNumber4,obj.SerialNumber5,obj.SerialNumber6,obj.SerialNumber7,obj.SerialNumber8,obj.SerialNumber9,obj.SerialNumber10,obj.Remark,obj.QualityJudgement,obj.AttachmentFile, obj.SetNumber);
            try{
                DB.ExecuteNonQuery(dbCommand);
                foreach (OControlTicketItemInfo itm in listItem)
                {
                    OControlTicketItem_Add(itm);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Delete(string TNO)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OControlTicket_Delete", TNO);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetByTNo(string TNO)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OControlTicket_GetByTNo", TNO);
            return DB.ExecuteReader(dbCommand);
	    }
      
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OControlTicket_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
		    DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(OControlTicketInfo obj, List<OControlTicketItemInfo> listItem)
        {
            try
            {
                Delete(obj.TNO);
                return   Add(obj, listItem);
            }
            catch
            {
                return false;
            }

        }
        public static int OControlTicketItem_Add(OControlTicketItemInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OControlTicketItem_Add",obj.TNO,obj.ItemCode,obj.ItemName,obj.Quantity,obj.Note);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static IDataReader OControlTicketItem_GetByTNO(string TNO)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OControlTicketItem_GetByTNO", TNO);
            return DB.ExecuteReader(dbCommand);
	    }
    }
}
